﻿@{
    ViewBag.Title = "订单支付";
    Layout = "~/Areas/Web/Views/Shared/_PayTopBar.cshtml";
    var orders = (IEnumerable<Himall.Model.OrderInfo>)ViewBag.Orders;
}
@model IEnumerable<Himall.Web.Areas.Web.Models.PaymentModel>
<div class="w990 main">
    <div class="m mainbody">
        <div class="mc">
            <s class="icon-succ04"></s>
            <h3 class="orderinfo">订单提交成功，请您尽快付款！</h3>
            <ul class="list-orderinfo">
                @foreach (var order in orders)
                {
                    <li>订单号：@order.Id</li>
                }
                <li class="li-last">应付金额：<strong class="ftx-01" id="totalAmount">@(((decimal)ViewBag.TotalAmount).ToString("F2"))</strong> 元</li>
            </ul>
            <p class="mb-tip">请您在提交订单后<span class="ftx-04">@(ViewBag.UnpaidTimeout)小时</span>内完成支付，否则订单会自动取消。</p>
        </div>
    </div>
    <div class="m pay-box">
        <div class="mc">
            <h5 class="subtit">使用预付款支付</h5>
            <label>
                <input type="radio" class="jdradio" value="" name="requestUrl" urltype="-1" id="" />
                <span>当前账户可用余额 <span id="capitalAmount"> @(ViewBag.Capital)</span>元</span>
                <a id="btnCharge" href="/userCenter?url=/usercapital/capitalcharge&tar=usercapital">去充值</a>
            </label>
        </div>
    </div>
    <div class="m pay-box">
        <div class="mc">
            <h5 class="subtit">请选择支付方式</h5>
            <ul class="list-bank">
                @{var i = 0;}
                @foreach (var payment in Model)
                {
                    <li>
                        <label>
                            <input type="radio" class="jdradio" value="@payment.RequestUrl" name="requestUrl" id="@payment.Id" urlType="@((int)payment.UrlType)" />
                            <img width="165" height="48" alt="微信PC" src="@payment.Logo" />
                        </label>
                    </li>
                }
            </ul>

        </div>
    </div>
     <div class="btns"><a class="btn-next" href="javascript:;" id="nextBtn">下一步</a></div>
</div>


<input type="hidden" id="orderIds" value="@ViewBag.OrderIds" />
<input type="hidden" id="payid" value="@ViewBag.PayId" />
<script>
    $(function () {
        $('.progress-').hide();
        var orderIds = $('#orderIds').val();
        $('input[name="requestUrl"]').change(function () {
            var url = $(this).val();
            if ($(this).attr('urlType') != '-1') {
                if ($(this).attr('urlType') == "1")
                    url = '/pay/QRPay?url=' + url + '&id=' + $(this).attr('id') + '&orderIds=' + orderIds;

                $('#nextBtn').attr('urlType', $(this).attr('urlType'));
                $('#nextBtn').attr('formdata', url);

                $('#nextBtn').removeAttr('href');
                $('#nextBtn').removeAttr('target');
                if ($(this).attr('urlType') != "2") {
                    $('#nextBtn').attr('href', url);
                    $('#nextBtn').attr('target', "_blank");
                }
            }
            else {
                $('#nextBtn').attr('urlType', $(this).attr('urlType'));
                $('#nextBtn').removeAttr('href');
            }
        });

        $('#nextBtn').click(function () {
            var t = $("input[name='requestUrl']:checked").val();
            if (t == undefined) {
                $.dialog.tips('请选择支付方式！');
                return;
            }
            if ($(this).attr('urlType') == "2") {
                var url = $(this).attr('formdata');
                BuildPostForm('pay_form', url, '_blank').submit();
            }

            if ($(this).attr('urlType') == "-1") {
                if (parseFloat($('#capitalAmount').text()) < parseFloat($('#totalAmount').text())) {
                    $.dialog.alert('预付款金额少于订单金额');
                    return false;
                }

                $.ajax({
                    type: 'post',
                    url: 'GetPayPwd',
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        if (!result.success)
                        {
                            window.top.open('/userCenter?url=/usercapital/setpaypwd&tar=setpaypwd', '_blank');
                        }
                    }
                });
                $.dialog({
                    title: '确认支付',
                    lock: true,
                    id: 'goodCheck',
                    content: ['<div class="dialog-form">',
                        '<div class="form-group">',
                           '<div class="item">\
                                 <span class="label">支付密码：</span>\
                                    <div class="">\
                                    <input type="password" value="" id="payPwd" name="userVo.realName" maxlength="20" class="itxt fl">\
                            </div>\
                            </div>',
                        '</div>',
                    '</div>'].join(''),
                    padding: '10px',
                    init: function () { $("#auditMsgBox").focus(); },
                    button: [
                    {
                        name: '付款',
                        callback: function () {
                            if ($("#payPwd").val().length == 0) {
                                $.dialog.alert("请输入支付密码");
                                return false;
                            }
                            $.post('PayByCapital', { orderIds: $('#orderIds').val(), pwd: $('#payPwd').val(), payid: $('#payid').val() }, function (result) {
                                if (result.success) {
                                    $.dialog.succeedTips(result.msg, function () {
                                        location.href = "/userCenter?url=/userorder&tar=userorder";
                                    });
                                }
                                else {
                                    $.dialog.alert(result.msg);
                                    return false;
                                }
                            });
                        },
                        focus: true
                    },
                    {
                        name: '取消',
                        callback: function () { }
                    }]
                });
            }
            else {
                if ($(this).attr('href') != 'javascript:;' || $(this).attr('urlType') == "2") {
                    $.dialog({
                        title: '登录平台支付',
                        lock: true,
                        content: '<p>请您在新打开的支付平台页面进行支付，支付完成前请不要关闭该窗口</p>',
                        padding: '30px 20px',
                        button: [
                        {
                            name: '已完成支付',
                            callback: function () {
                                location.href = '/userCenter?url=/userorder?orderids=@ViewBag.OrderIds&tar=userorder';
                            }
                        },
                        {
                            name: '支付遇到问题',
                            callback: function () { }
                        }]
                    });
                }
            }

        });

    });
    function BuildPostForm(fm, url, target) {
        var e = null, el = [];
        if (!fm || !url)
            return e;
        target = target || '_blank';
        e = document.getElementById(fm);
        if (!e) {
            e = document.createElement('Form');
            e.Id = fm;
            document.body.appendChild(e);
        }

        e.method = 'post';
        e.target = target;
        e.style.display = 'none';
        e.enctype = 'application/x-www-form-urlencoded';

        var idx = url.indexOf('?');
        var para = [], op = [];
        if (idx > 0) {
            para = url.substring(idx + 1, url.length).split('&');
            url = url.substr(0, idx);//截取URL
            var keypair = [];
            for (var p=0 ; p < para.length; p++) {
                idx = para[p].indexOf('=');
                if (idx > 0) {
                    el.push('<input type="hidden" name="' + para[p].substr(0, idx) + '" id="frm' + para[p].substr(0, idx) + '" value="' + para[p].substring(idx + 1, para[p].length) + '" />');
                }
            }
        }

        e.innerHTML = el.join('');
        e.action = url;
        return e;
    };
</script>